package com.gcb.controller;

import com.gcb.constant.MessageConstant;
import com.gcb.entity.PageResult;
import com.gcb.entity.QueryPageBean;
import com.gcb.entity.Result;
import com.gcb.pojo.CheckItem;
import com.gcb.service.CheckItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 35235
 * @Date 2021/8/3 17:41
 * @Version 1.0
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @DubboReference
    CheckItemService checkItemService;

    @RequestMapping(value = "/add.do")
    public Result add(@RequestBody CheckItem checkItem) {

        System.out.println("------------------已进入添加controller---------------------");
        int result = checkItemService.add(checkItem);
        System.out.println(result);
        if (result != 0) {
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } else {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);

        }
    }

    @RequestMapping("/hello")
    public void hello() {
        System.out.println("hahah");
    }

    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("----已进入查询页面----");
        PageResult pageResult = checkItemService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        System.out.println(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById.do")
    public Result findById(int id) {
        System.out.println(id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        System.out.println("---------已进入删除-------");
        int result = checkItemService.delete(id);
        try{
            System.out.println("result = " + result);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch(Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        System.out.println("已进入编辑的方法\n" + checkItem);
        int result = checkItemService.updateById(checkItem);
        if (0 != result){
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }else {
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}

